package com.mall.service.impl;

import com.mall.entity.Cart;
import com.mall.entity.CartItem;
import com.mall.entity.Product;
import com.mall.entity.User;
import com.mall.repository.CartRepository;
import com.mall.service.CartService;
import com.mall.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private CartRepository cartRepository;

    @Autowired
    private ProductService productService;

    @Override
    public Cart getCartByUser(User user) {
        return cartRepository.findByUser(user).orElse(null);
    }

    @Override
    public Cart createCart(User user) {
        Cart cart = new Cart();
        cart.setUser(user);
        return cartRepository.save(cart);
    }

    @Override
    @Transactional
    public CartItem addItemToCart(User user, Product product, int quantity) {
        Cart cart = getCartByUser(user);
        if (cart == null) {
            cart = createCart(user);
        }

        // 检查商品是否已在购物车中
        for (CartItem item : cart.getItems()) {
            if (item.getProduct().getId().equals(product.getId())) {
                item.setQuantity(item.getQuantity() + quantity);
                return cartRepository.save(cart).getItems().stream()
                        .filter(i -> i.getProduct().getId().equals(product.getId()))
                        .findFirst().orElse(null);
            }
        }

        // 新增购物车项
        CartItem cartItem = new CartItem();
        cartItem.setCart(cart);
        cartItem.setProduct(product);
        cartItem.setQuantity(quantity);
        cart.getItems().add(cartItem);

        return cartRepository.save(cart).getItems().stream()
                .filter(i -> i.getProduct().getId().equals(product.getId()))
                .findFirst().orElse(null);
    }

    @Override
    @Transactional
    public void updateCartItem(Long cartItemId, int quantity) {
        Cart cart = cartRepository.findByItems_Id(cartItemId).orElse(null);
        if (cart != null) {
            Optional<CartItem> itemOptional = cart.getItems().stream()
                    .filter(item -> item.getId().equals(cartItemId))
                    .findFirst();
            itemOptional.ifPresent(item -> {
                if (quantity <= 0) {
                    cart.getItems().remove(item);
                } else {
                    item.setQuantity(quantity);
                }
                cartRepository.save(cart);
            });
        }
    }

    @Override
    @Transactional
    public void removeCartItem(Long cartItemId) {
        Cart cart = cartRepository.findByItems_Id(cartItemId).orElse(null);
        if (cart != null) {
            cart.getItems().removeIf(item -> item.getId().equals(cartItemId));
            cartRepository.save(cart);
        }
    }

    @Override
    @Transactional
    public void clearCart(User user) {
        Cart cart = getCartByUser(user);
        if (cart != null) {
            cart.getItems().clear();
            cartRepository.save(cart);
        }
    }
}    